<template>
  <el-card>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }"
        ><i class="el-icon-s-home"></i> 首页</el-breadcrumb-item
      >
      <el-breadcrumb-item
        ><i class="el-icon-share"></i> 文章管理</el-breadcrumb-item
      >
      <el-breadcrumb-item
        ><i class="el-icon-share"></i> 文章分类</el-breadcrumb-item
      >
    </el-breadcrumb>
    <el-row type="flex" justify="end">
      <el-col :span="2">
        <el-button size="small" type="primary" @click="addArtCate"
          >添加</el-button
        >
      </el-col>
    </el-row>
    <!-- 添加分类信息的对话框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="addDialogVisible"
      width="35%"
      @close="closeAddDialog"
      ref="addRef"
    >
      <!-- 添加的表单 -->
      <el-form ref="addRef" label-width="80px" :model="addForm" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAddDialog">取 消</el-button>
        <el-button type="primary" @click="addCate">确定</el-button>
      </span>
    </el-dialog>
    <!-- _____________________________________________________ -->
    <!-- 修改分类信息的对话框 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="editDialogVisible"
      width="35%"
      @close="closeEditDialog"
      ref="addRef"
    >
      <!-- 添加的表单 -->
      <el-form ref="editRef" label-width="80px" :model="editform" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editform.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editform.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取 消</el-button>
        <el-button type="primary" @click="handelEditDialog">确定</el-button>
      </span>
    </el-dialog>
    <!-- 文章分类表格渲染 -->
    <template>
      <el-table :data="cateList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="170px">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column prop="date" label="操作">
          <template #default="{ row }">
            {{ row.id }}
            <el-button
              type="primary "
              size="small"
              @click="showEditCate(row.id)"
              >修改</el-button
            >
            <el-button type="danger" size="small" @click="delCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
  </el-card>
</template>

<script>
import { reqAddCate, reqDeleteCate, reqGetCateInfoById, reqUpdataArticle } from '@/api/article'

import { mapState } from 'vuex'
export default {
  name: 'ArtCategory',
  data () {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      // 新增文章分类的数据
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editform: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  created () {
    // 获取文章分类
    this.$store.dispatch('article/GetCateList')
  },
  methods: {
    addArtCate () {
      this.addDialogVisible = true
    },
    // 对话框关闭
    closeAddDialog () {
      this.addDialogVisible = false
      this.$refs.addRef.resetFields()
    },
    // 修改框关闭
    closeEditDialog () {
      this.editDialogVisible = false
    },
    // 添加分类
    addCate () {
      this.$refs.addRef.validate(async (boo) => {
        if (boo) {
          const { data } = await reqAddCate(this.addForm)
          if (data.code !== 0) return this.$message.danger(data.message)
          this.$message.success(data.message)
          this.closeAddDialog()
          // 获取文章分类
          this.$store.dispatch('article/GetCateList')
        }
      })
    },
    // 删除功能
    async delCate (id) {
      if (id === 1 || id === 2) return this.$message.error('禁止删除默认分类')
      const str = await this.$confirm(
        '此操作将会删除本文章，是否继续?',
        '提示',
        {
          type: 'error'
        }
      ).catch(() => {})
      console.log(str)
      // 取消了删除
      // 发送请求
      const { data } = await reqDeleteCate(id)
      // console.log(res)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      // 获取文章分类
      this.$store.dispatch('article/GetCateList')
    },
    // 修改功能
    async showEditCate (id) {
      console.log(id)
      if (id === 1 || id === 2) return this.$message.error('禁止修改此分类')
      this.editDialogVisible = true
      const { data } = await reqGetCateInfoById(id)
      // console.log(data)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      // 回显
      this.editform = data.data
    },
    handelEditDialog () {
      this.$refs.editRef.validate(async boo => {
        // console.log(boo)
        if (boo) {
          // 发请求
          const { data } = await reqUpdataArticle(this.editform)
          if (data.code !== 0) return this.$message.error(data.message)
          this.$message.success(data.message)
          this.editDialogVisible = false
          this.$store.dispatch('article/GetCateList')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
  /deep/ .el-table__header-wrapper {
    .cell {
      color: #000 !important;
    }
  }
}
</style>
